﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI;
using BLL;
using Model;
using Wuqi.Webdiyer;

namespace WebApplication1.InfoManagement
{
    public partial class StockWarnManagement : System.Web.UI.Page
    {
        private string var_RoomName
        {
            get { return ViewState["RoomName"].ToString(); }
            set { ViewState["RoomName"] = value; }
        }
        private string var_Tool_Code
        {
            get { return ViewState["Tool_Code"].ToString(); }
            set { ViewState["Tool_Code"] = value; }
        }

        private StockWarn sw;
        private ToolRoom tr;

        public StockWarnManagement()
        {
            sw = new StockWarn();
            tr = new ToolRoom();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindToolRoom();
                var_RoomName = "请选择";
                var_Tool_Code = "";
                BindStockWarn();
            }
            Delall.Attributes.Add("onclick", "return confirm('你确认要删除这批数据!')"); 
        }

        private void BindToolRoom()
        {
            IList<ToolRoomInfo> list = tr.LoadEntities();
            IList<string> str_list = new List<string>();
            foreach (ToolRoomInfo tri in list)
            {
                if (!str_list.Contains(tri.RoomName))
                    str_list.Add(tri.RoomName);
            }
            this.DropDownList1.DataSource = str_list;
            this.DropDownList1.DataBind();
            this.DropDownList1.Items.Insert(0, new ListItem("请选择"));
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var_RoomName = this.DropDownList1.SelectedItem.Text;
            var_Tool_Code = this.TextBox1.Text.Trim();
            BindStockWarn();
        }

        private void BindStockWarn()
        {
            IList<StockWarnInfo> list = sw.StockWarn_GetbyMultiFact(var_RoomName.Equals("请选择") ? null : var_RoomName, string.IsNullOrEmpty(var_Tool_Code) ? null : var_Tool_Code);
            this.Lbl_RecordsCount.Text = list.Count.ToString();
            //this.GridView1.DataSource = list;
            //this.GridView1.DataBind();
            //初始化分页数据源实例
            PagedDataSource pds = new PagedDataSource();
            //设置总行数
            AspNetPager1.RecordCount = list.Count;
            //设置分页的数据源
            pds.DataSource = list;
            //设置当前页
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            //设置每页显示页数
            pds.PageSize = AspNetPager1.PageSize;
            //启用分页
            pds.AllowPaging = true;
            //设置GridView的数据源为分页数据源
            GridView1.DataSource = pds;
            //绑定GridView
            GridView1.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindStockWarn();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

            GridView1.EditIndex = e.NewEditIndex;
            BindStockWarn();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
          
            GridView1.EditIndex = -1;
            BindStockWarn();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int amount_Warn;
            if (!int.TryParse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.Trim(), out amount_Warn))
            {
                string str = "预警数量项，必须录入整数！";
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
                return;
            }
            StockWarnInfo swi = new StockWarnInfo();
            swi.Tool_Code = ((Label)(GridView1.Rows[e.RowIndex].FindControl("Lbl_ToolCode"))).Text.Trim();
            swi.Amount_Warn = amount_Warn;
            if (GridView1.DataKeys[e.RowIndex].Values.Count==0)
            {
                swi.ID = 0;
                sw.Insert(swi);
            }
            else
            {
                swi.ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
                sw.Update(swi);
            }
            GridView1.EditIndex = -1;
            BindStockWarn();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    int amount = int.Parse(DataBinder.Eval(e.Row.DataItem, "Amount").ToString());
                    object amount_Warn_str = DataBinder.Eval(e.Row.DataItem, "Amount_Warn");
                    if (amount_Warn_str != null)
                    {
                        int amount_Warn = int.Parse(amount_Warn_str.ToString());
                        if (amount <= amount_Warn)
                            e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
                    }
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/InfoManagement/StockWarnAdd.aspx");
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Delall_Click(object sender, EventArgs e)
        {
            int intCount = this.GridView1.Rows.Count;
            for (int i = 0; i < intCount; i++)
            {
                CheckBox CheckSingle = this.GridView1.Rows[i].Cells[0].FindControl("CheckSingle") as CheckBox;
                if (CheckSingle.Checked)
                {
                    int ID = int.Parse(GridView1.DataKeys[i].Values[0].ToString());
                    sw.Delete(ID);
                }
            }
            //删除后的更新
            BindStockWarn();
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            //调用绑定分页和GridView
            AspNetPager1.CurrentPageIndex =(sender as AspNetPager).CurrentPageIndex;
            BindStockWarn();
        }
    }
}
